package com.gxy.learn.thread.example;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description: 多线程同步等待
 * @author: gaoxueyong
 * @Date: 2022/11/10 12:00
 */
public class CountDownLatchAndJonAndCyclicBarrierTest {
    public static void main(String[] args) {
//        countDownLatchTest2();
//        countDownLatchTest();
//        System.out.println("=====");
//        joinTest();
        cyclicBarrierTest();
    }

    public static void joinTest() {
        Thread[] threads = new Thread[100];
        CountDownLatch countDownLatch = new CountDownLatch(threads.length);

        for (int i = 0; i < threads.length; i++) {
            int finalI = i;
            threads[i] = new Thread(() -> {
                int result = 0;
                for (int j = 0; j < 10000; j++) result += j;
                System.out.println("线程"+finalI+"已完成");
                countDownLatch.countDown();
            });
        }
        for (int j = 0; j < threads.length; j++) {
            threads[j].start();
        }
        for (int j = 0; j < threads.length; j++) {
            try {
                threads[j].join();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("所有线程已全部执行");
    }

    public static void countDownLatchTest() {
        Thread[] threads = new Thread[100];
        CountDownLatch countDownLatch = new CountDownLatch(threads.length);

        for (int i = 0; i < threads.length; i++) {
            int finalI = i;
            threads[i] = new Thread(() -> {
                int result = 0;
                for (int j = 0; j < 10000; j++) result += j;
                System.out.println("线程"+finalI+"已完成");
                countDownLatch.countDown();
            });
        }
        for (int j = 0; j < threads.length; j++) {
            threads[j].start();
        }
        try {
            countDownLatch.await();
            System.out.println("所有线程已全部执行");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void countDownLatchTest2() {
        Thread[] threads = new Thread[10];
        CountDownLatch countDownLatch = new CountDownLatch(threads.length);

        for (int i = 0; i < threads.length; i++) {
            int finalI = i;
            threads[i] = new Thread(() -> {
                try{
                    Thread.sleep(2000);
                }catch (Exception e){
                    e.printStackTrace();
                }
                System.out.println("线程"+finalI+"已完成");
                countDownLatch.countDown();
            });
        }
        for (int j = 0; j < threads.length; j++) {
            threads[j].start();
        }
        try {
            countDownLatch.await();
            System.out.println("所有线程已全部执行");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void cyclicBarrierTest() {
        Thread[] threads = new Thread[10];
        CyclicBarrier cyclicBarrier = new CyclicBarrier(threads.length, () -> {
            System.out.println("所有线程均完成，此处进行栅栏后的收尾工作");
        });

        for (int i = 0; i < threads.length; i++) {
            int finalI = i;
            threads[i] = new Thread(() -> {
                try {
                    System.out.println("线程 " + finalI + " 开始工作");
                    Thread.sleep(new Random().nextInt(10) * 1000);
                    System.out.println("线程 " + finalI + " 到达栅栏");
                    cyclicBarrier.await();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            });
        }
        for (int j = 0; j < threads.length; j++) {
            threads[j].start();
        }

    }
}
